'use strict';

const BaseService = require('./base');

class DeviceService extends BaseService {
  get _model() {
    return 'Device';
  }

  async findOne({ id }) {
    const { app } = this;
    const where = { id };
    const include = [
      {
        model: app.model.Device,
        as: 'parentDevice',
        attributes: [ 'name' ],
      },
    ];
    return this._findOne({ where, include });
  }

  async create({ name, parentId }) {
    return this._create({
      name,
      parentId,
    });
  }

  async update({ name, id }) {
    const where = { id };
    const body = { name };
    return this._update({ body, where });
  }

  async destory(id) {
    const where = { id };
    return this._destory({ where });
  }

  async findList({ name, prop, sort, pageIndex, pageSize, createdAt }) {
    const { app } = this;
    const where = this.formatWhere({ dim: { name }, range: { createdAt } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.Device,
        as: 'parentDevice',
        attributes: [ 'name' ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, order, include });
  }

  async findAll({ ids, parentId } = {}) {
    const where = this.formatWhere({ inOp: { id: ids }, isNull: { parentId } });

    return this._findAll({ where });
  }
}

module.exports = DeviceService;
